import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../main.dart';

class FormDemo extends StatelessWidget {
  const FormDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('form')),
      body: Theme(
          data: Theme.of(context).copyWith(primaryColor: Colors.black),
          child: Container(
            padding: EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [RegisterForm()],
            ),
          )),
    );
  }
}

class TextfieldDemo extends StatefulWidget {
  TextfieldDemo({Key key}) : super(key: key);

  @override
  _TextfieldDemoState createState() => _TextfieldDemoState();
}

class _TextfieldDemoState extends State<TextfieldDemo> {
  final textEditingController = TextEditingController();

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    textEditingController.addListener(() {
      debugPrint('controllerInput: ${textEditingController.text}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextField(
        controller: textEditingController,
        //  onChanged: (value){
        //    debugPrint('input: $value');
        //  },
        onSubmitted: (value) {
          debugPrint('submit: $value');
        },
        decoration: InputDecoration(
            icon: Icon(Icons.subject),
            labelText: 'Title',
            hintText: 'Enter the text',
            border: OutlineInputBorder(),
            filled: true),
      ),
    );
  }
}

class RegisterForm extends StatefulWidget {
  RegisterForm({Key key}) : super(key: key);
  @override
  _RegisterFormState createState() => _RegisterFormState();
}

class _RegisterFormState extends State<RegisterForm> {
  // 创建一个native的channel(与原生通信)
  static const methodChannel =
      const MethodChannel('com.test.wangxh/navtive_action');
  _iOSPushToVC() async {
    await methodChannel.invokeMethod('iOSFlutter', '10086');
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      child: Column(
        children: <Widget>[
          TextFormField(decoration: InputDecoration(labelText: 'UserName')),
          TextFormField(
              obscureText: true,
              decoration: InputDecoration(labelText: 'Password')),
          SizedBox(height: 32.0),
          Container(
              width: double.infinity,
              child: RaisedButton(
                  color: Theme.of(context).accentColor,
                  child:
                      Text('Register', style: TextStyle(color: Colors.white)),
                  elevation: 0.0,
                  onPressed: () {
                    _iOSPushToVC();
                  }))
        ],
      ),
    );
  }
}

class ThemeDemo extends StatelessWidget {
  const ThemeDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).accentColor,
    );
  }
}
